var GAME_RECT_WIDTH = 256;

var BOARD_WIDTH = GAME_RECT_WIDTH;
var BOARD_HEIGHT = GAME_RECT_WIDTH;

var FILL_TEX_WIDTH = BOARD_WIDTH;
var FILL_TEX_HEIGHT = BOARD_HEIGHT;

//var BG_COLOR = 0x00000000;//ARGB
var BG_COLOR = [0, 0, 0, 0];//ARGB
//var FAIL_COLOR = 0x88ffffff;
var FAIL_COLOR = [255, 255, 255, 180];
//var PASS_COLOR = 0xFFFFFFFF;
var PASS_COLOR = [255, 255, 255, 255];

var m_colorId = new Array(BOARD_WIDTH);//关卡颜色，-1表示线
var m_colorRGB = new Array(FILL_TEX_WIDTH);//用于生成填充贴图的rgb
var m_colorCheckRGB = new Array(BOARD_WIDTH);//用于检查的已填充的rgb，填充的是colorMark的相应颜色
//var m_colorMark = new Array(4);//四种颜色标记
var m_colorMark = new Array(0xFFFCCD05, 0xFF05CD98, 0xFF3399FF, 0xFF8A05F7);//四种颜色标记
var m_failColorRGB = new Array(BOARD_WIDTH);

var m_colorTextures = new Array(4);

var m_allColorTextures = new Object();

var needFillNum = 0;

var g_filledColorSpr = null;
var g_flashRegionSpr = null;

var mFillCount = 0;
var mColorUsed = 0;

var tempImageCanvas = document.getElementById("tempImageCanvas");
var ctx = tempImageCanvas.getContext("2d");
var tempFailImageCanvas = document.getElementById("tempFailImageCanvas");
var fail_ctx = tempFailImageCanvas.getContext("2d");
var imgData = null;
var failImgData = null;

var LevelHelper = {
    init: function(){
        g_filledColorSpr = new cc.Sprite();
        g_flashRegionSpr = new cc.Sprite();
    }
}

function makeFilledColorSprite(){
    ctx.putImageData(imgData, 0, 0);
    var image = new Image();
    image.src = tempImageCanvas.toDataURL();
    image.width = FILL_TEX_WIDTH;
    image.height = FILL_TEX_HEIGHT;
    var texture2d = new cc.Texture2D();
    texture2d.initWithElement(image);
    texture2d.handleLoadedTexture();
    var textureSize = texture2d.getContentSize().width;
    g_filledColorSpr.setDisplayFrame(cc.SpriteFrame.createWithTexture(texture2d, cc.rect(0, 0, textureSize, textureSize)));
    
}

function makeFlashSprite(){
    fail_ctx.putImageData(failImgData, 0, 0);
    var image = new Image();
    image.src = tempFailImageCanvas.toDataURL();
    image.width = FILL_TEX_WIDTH;
    image.height = FILL_TEX_HEIGHT;
    var texture2d = new cc.Texture2D();
    texture2d.initWithElement(image);
    texture2d.handleLoadedTexture();
    var textureSize = texture2d.getContentSize().width;
    g_flashRegionSpr.setDisplayFrame(cc.SpriteFrame.createWithTexture(texture2d, cc.rect(0, 0, textureSize, textureSize)));
    
}

function loadLevel(file){
    var fileUtils = cc.FileUtils.getInstance();
    var path = fileUtils.fullPathFromRelativePath(file);
    var data = fileUtils.getByteArrayFromFile(path);
    
    needFillNum = data[0];
    var txt = "";
    var len = 1;
    for (var i = 0; i < GAME_RECT_WIDTH; i++) {
        m_colorId[i] = new Array(BOARD_WIDTH);
        //txt += "\n"
        for (var j = 0; j < GAME_RECT_WIDTH; j++) {
            m_colorId[i][j] = data[len++];
            if (m_colorId[i][j] == 255) {
                m_colorId[i][j] = -1;
                //txt += "8"
            } else {
                //txt += "-"
            }
        }
    }
    cc.log("load level file:" + path + ", needFillNum:" + needFillNum);
    //cc.log(txt)
}

function resetLevel(){
    for (var i = 0; i < BOARD_HEIGHT; i++) {
        m_colorCheckRGB[i] = new Array(BOARD_HEIGHT);
        m_failColorRGB[i] = new Array(BOARD_HEIGHT);
        for (var j = 0; j < BOARD_WIDTH; j++) {
            m_colorCheckRGB[i][j] = BG_COLOR;
            m_failColorRGB[i][j] = 0;
        }
    }
    imgData = ctx.createImageData(FILL_TEX_WIDTH, FILL_TEX_HEIGHT);
    failImgData = fail_ctx.createImageData(FILL_TEX_WIDTH, FILL_TEX_HEIGHT);
    makeFilledColorSprite();
    makeFlashSprite();
    
    mFillCount = 0;
    mColorUsed = 0;
    
    cc.log("reset level");
}

//此方法废弃
function setColorTexture_bak(index, texName){
    var textureName = texName;
    if (index >= 0 && index < 4) {
        if (m_allColorTextures.textureName != undefined) {
            m_colorTextures[index] = m_allColorTextures[textureName];
        } else {
            var img = new cc.Image();
            var fileUtils = cc.FileUtils.getInstance();
            var path = fileUtils.fullPathFromRelativePath(textureName);
            var data = fileUtils.getByteArrayFromFile(path);
            
            if (data != null && data.length > 0) {
                img._data = data;
            }
            m_colorTextures[index] = img;
        }
    }
}

function setColorTexture(index, texName){
    var textureName = texName;
    if (index >= 0 && index < 4) {
        if (m_allColorTextures.textureName != undefined) {
            m_colorTextures[index] = m_allColorTextures[textureName];
        } else {
            m_colorTextures[index] = textureName;
        }
    }
}

function unLoadLevel(){
    for (var i = 0; i < 4; i++) {
        if (m_colorTextures[i]) {
            m_colorTextures[i] = null;
        }
    }
}

function fillColor(m, n, colorIndex, isTutorial){
    var color_id = m_colorId[parseInt(m)][parseInt(n)];
    mFillCount++;
    var colorTextureData = m_colorTextures[colorIndex];
    for (var i = 0; i < BOARD_WIDTH; i++) {
        for (var j = 0; j < BOARD_HEIGHT; j++) {
            if (m_colorId[i][j] >= 0 && m_colorId[i][j] == color_id) {
                if (isTutorial && m_colorCheckRGB[i][j] != BG_COLOR) {
                    return;
                }
                
                if (m_colorCheckRGB[i][j] != m_colorMark[colorIndex]) {
                    imgData.data[(i * FILL_TEX_WIDTH + j) * 4 + 0] = colorTextureData[0];//R
                    imgData.data[(i * FILL_TEX_WIDTH + j) * 4 + 1] = colorTextureData[1];//G
                    imgData.data[(i * FILL_TEX_WIDTH + j) * 4 + 2] = colorTextureData[2];//B
                    imgData.data[(i * FILL_TEX_WIDTH + j) * 4 + 3] = 255;//A
                    m_colorCheckRGB[i][j] = m_colorMark[colorIndex];
                } else {
                    imgData.data[(i * FILL_TEX_WIDTH + j) * 4 + 0] = BG_COLOR[0];
                    imgData.data[(i * FILL_TEX_WIDTH + j) * 4 + 1] = BG_COLOR[1];
                    imgData.data[(i * FILL_TEX_WIDTH + j) * 4 + 2] = BG_COLOR[2];
                    imgData.data[(i * FILL_TEX_WIDTH + j) * 4 + 3] = BG_COLOR[3];
                    m_colorCheckRGB[i][j] = BG_COLOR;
                }
            }
        }
    }
    makeFilledColorSprite();
}

function checkPassLevel(){
    for (var i = 0; i < BOARD_WIDTH; i++) {
        for (var j = 0; j < BOARD_HEIGHT; j++) {
            if (m_colorId[i][j] == -1) {
                if (j > 0 && j < BOARD_HEIGHT - 1) {
                    if ((m_colorCheckRGB[i][j - 1] == m_colorCheckRGB[i][j + 1]) &&
                    (m_colorId[i][j - 1] != -1) &&
                    (m_colorId[i][j + 1] != -1) &&
                    (m_colorId[i][j - 1] != m_colorId[i][j + 1])) {
                        cc.log("[1] i=" + i + " j=" + j);
                        return false;
                    }
                }
                if (i > 0 && i < BOARD_WIDTH - 1) {
                    if ((m_colorCheckRGB[i - 1][j] == m_colorCheckRGB[i + 1][j]) &&
                    (m_colorId[i - 1][j] != -1) &&
                    (m_colorId[i + 1][j] != -1) &&
                    (m_colorId[i - 1][j] != m_colorId[i + 1][j])) {
                        cc.log("[2] i=" + i + " j=" + j);
                        return false;
                    }
                }
            } else if (m_colorCheckRGB[i][j] == BG_COLOR) {
                //cc.log("[3] i=" + i + " j=" + j);
                return false;
            }
        }
    }
    
    for (var i = 0; i < BOARD_HEIGHT; i++) {
        for (var j = 0; j < BOARD_WIDTH; j++) {
            failImgData.data[(i * FILL_TEX_WIDTH + j) * 4 + 0] = PASS_COLOR[0];//R
            failImgData.data[(i * FILL_TEX_WIDTH + j) * 4 + 1] = PASS_COLOR[1];//G
            failImgData.data[(i * FILL_TEX_WIDTH + j) * 4 + 2] = PASS_COLOR[2];//B
            failImgData.data[(i * FILL_TEX_WIDTH + j) * 4 + 3] = PASS_COLOR[3];//A
        }
    }
    
    makeFlashSprite();
    
    var colorUsed = new Array();
    //compute color used
    for (var i = 0; i < BOARD_WIDTH; i++) {
        for (var j = 0; j < BOARD_HEIGHT; j++) {
            if ((m_colorCheckRGB[i][j] != BG_COLOR)) {
                colorUsed[m_colorCheckRGB[i][j]] = 1;
            }
        }
    }
    
    cc.log("check level pass. needFillNum=" + needFillNum + ", mFillCount=" + mFillCount + ", mColorUsed=" + colorUsed.length);
    
    return true;
}

function checkFailRegion(){
    for (var i = 0; i < BOARD_WIDTH; i++) {
        for (var j = 0; j < BOARD_HEIGHT; j++) {
            if (m_colorId[i][j] >= 0 && m_colorCheckRGB[i][j] == BG_COLOR) {
                //cc.log("not finished, so not fail i=" + i + " j=" + j);
                return false;//not finished, so not fail
            }
        }
    }
    
    var failColorId1 = -1;
    var failColorId2 = -1;
    
    function end(){
        if (failColorId1 >= 0 && failColorId2 >= 0) {
            for (var i = 0; i < BOARD_WIDTH; i++) {
                for (var j = 0; j < BOARD_HEIGHT; j++) {
                    if (m_colorId[i][j] >= 0 && (m_colorId[i][j] == failColorId1 || m_colorId[i][j] == failColorId2)) {
                        failImgData.data[(i * FILL_TEX_WIDTH + j) * 4 + 0] = FAIL_COLOR[0];//R
                        failImgData.data[(i * FILL_TEX_WIDTH + j) * 4 + 1] = FAIL_COLOR[1];//G
                        failImgData.data[(i * FILL_TEX_WIDTH + j) * 4 + 2] = FAIL_COLOR[2];//B
                        failImgData.data[(i * FILL_TEX_WIDTH + j) * 4 + 3] = FAIL_COLOR[3];//A
                    } else {
                        failImgData.data[(i * FILL_TEX_WIDTH + j) * 4 + 0] = BG_COLOR[0];//R
                        failImgData.data[(i * FILL_TEX_WIDTH + j) * 4 + 1] = BG_COLOR[1];//G
                        failImgData.data[(i * FILL_TEX_WIDTH + j) * 4 + 2] = BG_COLOR[2];//B
                        failImgData.data[(i * FILL_TEX_WIDTH + j) * 4 + 3] = BG_COLOR[3];//A
                    }
                }
            }
            makeFlashSprite();
            return true;
        } else {
            return false;
        }
    }
    
    for (var i = 0; i < BOARD_WIDTH; i++) {
        for (var j = 0; j < BOARD_HEIGHT; j++) {
            if (m_colorId[i][j] == -1) {
                if (j > 0 && j < BOARD_HEIGHT - 1) {
                    if ((m_colorCheckRGB[i][j - 1] == m_colorCheckRGB[i][j + 1]) &&
                    (m_colorId[i][j - 1] != -1) &&
                    (m_colorId[i][j + 1] != -1) &&
                    (m_colorId[i][j - 1] != m_colorId[i][j + 1])) {
                        failColorId1 = m_colorId[i][j - 1];
                        failColorId2 = m_colorId[i][j + 1];
                        return end();
                    }
                }
                if (i > 0 && i < BOARD_WIDTH - 1) {
                    if ((m_colorCheckRGB[i - 1][j] == m_colorCheckRGB[i + 1][j]) &&
                    (m_colorId[i - 1][j] != -1) &&
                    (m_colorId[i + 1][j] != -1) &&
                    (m_colorId[i - 1][j] != m_colorId[i + 1][j])) {
                        failColorId1 = m_colorId[i - 1][j];
                        failColorId2 = m_colorId[i + 1][j];
                        return end();
                    }
                }
            }
        }
    }
}
